<template>
  <!-- 大客户线索管理->经销商大客户区域经理维护 -->
  <div>
    <!-- 搜索 -->
    <TopFilter  
      ref="conditionFilter"
      :list="topFilterInit"  
      :cols="3"  
      labelWidth="100"  
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
      ></TopFilter>
     <!-- table -->
    <FilterTable  
        ref="table" 
       :columns="columns"   
       :fetchapi="fetchapi" 
       :isSelectColumn="isSelectColumn"
       :params="fetchParams" 
       :datakey="'records'"
       columnsRef='customerManage_clue_areaBossOperation_index_01'
       :onColumnsChange="columns => this.columns = columns">
      <template slot="controls" >
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>
        <el-button size="small" type="primary" icon="el-icon-download" :loading="exportLoading" @click="exportData" >下载</el-button>
      </template>
   </FilterTable>
    <!-- 经销商弹窗 -->
  <BaseDialog title="经销商查询" :visible.sync="visibleDealer"
      width="1000px"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}" >
      <dealer-madal  ref="addDialog" selectType="multiple" v-model="initDealerData" @close="closeHandler" />
   </BaseDialog>
   <Drawer  :visible.sync="visibleDrawer"
      title="大客户区域经理维护"
      :width="920"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <info-modal  :managerId="managerId" :formType="formType" @close="infoClose" />
   </Drawer>
   
  </div>
</template>
<script>
import dealerMadal from '@/pages/cms-common/dealerdata/index.vue'
import infoModal from './info'
import { dictionary } from '@/utils/dictMixin';
import { PHONE_PATTERN } from '@/utils/validate' // 验证
import { Notification } from 'element-ui';
import { exportExcelFile, formatdateByDay } from '@/utils';
import { getManagerList, getFilterManager , exportManagerList} from '@/api/cms-api/customerManage/clue/areaBossOperation';
export default {
  name: 'customerAdd',
  mixins: [dictionary],
  components: {
    infoModal,dealerMadal
  },
  data() {
    return {
      visibleDrawer:false,
      visibleDealer:false,
      isSelectColumn:false,
      managerId:null,
      formType:'add',
      initDealerData:{},//经销商选择框数据初始化
      topFilterInit:this.topFilterInitFun(),
      fetchapi: getManagerList,
      columns:this.creatHeader(),
      fetchParams:null, //ipa 接口
      dealerCode:{},//经销商编码
      exportLoading: false
    };
  },
   mounted() {
     this.initAreaManager();
  //   this.BaseTable = this.$refs.table.$refs.pageTable;
   },
  methods: {
      //搜索数据
      topFilterInitFun(){
        return [
            { type: 'SELECT',   label: '大客户事业部', fieldName: 'majorManagerId', placeholder: '请选择',initialValue: '', itemList:  this.createDictList('2566') },
            { type: 'SELECT',   label: '大客户区域经理', fieldName: 'userId', placeholder: '请选择',initialValue: '' ,filterable:true,
                itemList:[]},
            { type: 'INPUT',  label: '经销商', fieldName: 'dealerShowName',  placeholder: '请选择', initialValue: '', 
             unitRender: () => {
                    return (
                  <div>
                     <el-button slot="append" icon="el-icon-search" onClick={() =>this.dealerHandle()}></el-button>
                     <el-button slot="append" icon="el-icon-delete" onClick={() => this.clearSelectDealear()}></el-button>
                  </div>
                ); 
              },
            },
            { type: 'SELECT',  label: '状态', fieldName: 'status',  placeholder: '请选择', initialValue: "", itemList:  this.createDictList('1001') }
        ]
      },
      //搜索获取数据
      changeHandle(val){
         if(!val || val.dealerShowName==null || val.dealerShowName==''){
              this.dealerCode = '';
          }
          let extParams = {'dealerCodes':this.dealerCode};
        this.fetchParams=Object.assign({},val,extParams);
      },
      //table表头和数据源
      creatHeader(){
         return [
         {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
         },
         {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>编辑</el-button>
              </div>
            );
          }
        },
        { title: '状态',  dataIndex: 'status', sorter: true,
           render: props => {    //编码转汉字
            return <span>{this.createDictText(props.row.status, '1001')}</span>;
          }},
        { title: '大客户事业部',  dataIndex: 'majorManagerId', sorter: true,
           render: props => {    //编码转汉字
            return <span>{this.createDictText(props.row.majorManagerId, '2566')}</span>;
          }},
        { title: '区域经理代码',      dataIndex: 'employeeNo', sorter: true},
        { title: '区域经理姓名',   dataIndex: 'majorSbuManagerName', sorter: true},
        { title: '性别',    dataIndex: 'gender',  sorter: true,
           render: props => {    //编码转汉字
            return <span>{this.createDictText(props.row.gender, '1002')}</span>;
          }},
        { title: '身份证号码',  dataIndex: 'idCardNum',   sorter: true},
        { title: '手机',  dataIndex: 'phone', sorter: true},
        { title: '下属经销商数量',  dataIndex: 'dealerTotalNum',   sorter: true },
        { title: '创建时间',  dataIndex: 'createdAt',  sorter: true,
          render: props =>{
            return <span>{ formatdateByDay(props.row.createdAt) }</span>
          } },
        { title: '更新时间',  dataIndex: 'updatedAt', sorter: true,
          render: props =>{
            return <span>{ formatdateByDay(props.row.updatedAt) }</span>
          } }
        ]
      },
      clearSelectDealear(){
        this.topFilterInit.find(item => item.fieldName === 'dealerShowName').initialValue = '';
        this.dealerCode = '';
      },
      // 经销商打开窗口 -传数据
      dealerHandle(){
        this.initDealerData =  {
          excludeBigCustomer: true , // 是否查询大客户经销商，如果查询 true，不查询 false
          id:10011010
        }; //行id
        this.visibleDealer=true
      },
      // 经销商关闭窗口 -返回数据
      closeHandler(data){
        if(data.data!=null && typeof(data.data)!='undefined' && data.data.length>0){
            //返回数据逻辑自己写
            let dealerNames = [];
            let dealerCodes = [];
            (data.data).forEach(dealer => {
              dealerNames.push(dealer.dealerName);
              dealerCodes.push('\''+dealer.dealerCode+'\'');
            });
            this.topFilterInit.find(item => item.fieldName === 'dealerShowName').initialValue = dealerNames.join(",");
            this.dealerCode = dealerCodes.join(",");
        }
        this.visibleDealer=data.visible //关闭弹窗
    },
    //编辑
    editHandle(row){
      this.formType = 'edit';
      this.managerId = row['managerId']; //行id
      this.visibleDrawer=true
    },
    //新增
    addHandle(row){
      this.formType = 'add';
      this.managerId = row['managerId']; //行id
      this.visibleDrawer=true
    },
    //关闭详情
    infoClose(data){
      this.visibleDrawer=false
      if (data.type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
     collapseHandle() {
      this.$nextTick(() => {
        //this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    exportData(){
      this.$refs.conditionFilter.SUBMIT_FORM();
      this.exportLoading = true
      exportManagerList(this.fetchParams).then(res => {
        exportExcelFile(res.data,'大客户线索区域经理')
        this.exportLoading = false
      });
    },
    //初始化区域经理选择框
    async initAreaManager(){
      let managerList = await getFilterManager();
      let initList = new Array();
      let userIds = [];
      if(managerList!=null && managerList.data.length>0){
        managerList.data.forEach(mData => {
          let uid = mData.userId;
          if(uid!=null && uid!='' && userIds.indexOf(uid) == -1){
            userIds.push(uid);
            let selectObj = new Object();
            selectObj.text = mData['majorSbuManagerName'];
            selectObj.value = mData['userId'];
            initList.push(selectObj);
          }
        });
      }
      this.topFilterInit[1].itemList = initList;
    }
  }
};
</script>

<style lang="less" scoped>
</style>
